<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 6/27/21
 * Time: 11:05 PM
 */

namespace app\api\controller\v1;


use app\api\model\ProductOrder;
use app\api\model\Store;
use app\api\model\StoreUser as StoreUserModel;
use app\api\model\TeamUser as TeamUserModel;
use app\api\model\User as UserModel;
use app\api\model\UserInvitation;

class NewUser
{
    // 新客数排行按照门店
    public function getNewUserByStore($start_time,$end_time)
    {
        $where = [];
        if (!$start_time) {
            $start_time = strtotime("-1 month");
            $end_time = time();
            $where[] = ['pay_time', 'egt', $start_time];
            $where[] = ['pay_time', 'elt', $end_time];
        } else {
            $where[] = ['pay_time', 'egt', strtotime($start_time." 00:00:00")];
            $where[] = ['pay_time', 'elt', strtotime($end_time." 23:59:59")];
        }
        $store  = Store::order('id desc')->select()->toArray();
        foreach ($store as $k => $v){
            $store[$k]['count'] = 0;
            $userInviteOne = UserInvitation::where('type',2)->select()->toArray();
            $idsOne = array_column($userInviteOne,'invitee');
            $whereFour = [];
            $whereFour[] = ['id','in',$idsOne];
            $whereFour[] = ['status','eq',3];
            $whereFour[] = ['new_type','eq',1];
            $whereFour[] = ['store_id','eq',$v['id']];
            $userOne = UserModel::where($whereFour)->select()->toArray();
            foreach ($userOne as $ka=>$va){
                $whereFive = $where;
                $whereFive[] = ['user_id','eq',$userOne[$ka]['id']];
                $whereFive[] = ['status', 'gt', 1];
                $order = ProductOrder::where($whereFive)->find();
                if($order) {
                    $store[$k]['count'] += 1;
                }
            }
        }
        $count = 0;
        $whereOne = [];
        $whereOne[] = ['type','eq',1];
        $userInvite = UserInvitation::where($whereOne)->select()->toArray();
        $ids = array_column($userInvite,'invitee');
        $whereTwo = [];
        $whereTwo[] = ['id','in',$ids];
        $whereTwo[] = ['status','eq',3];
        $user = UserModel::where($whereTwo)->select()->toArray();
        foreach ($user as $ka=>$va){
            $whereThree = $where;
            $whereThree[] = ['user_id','eq',$user[$ka]['id']];
            $whereThree[] = ['status', 'gt', 1];
            $order = ProductOrder::where($whereThree)->find();
            if($order) {
                $count += 1;
            }
        }
        array_push($store,[ 'id' => -1,'name' => '顾客新客数', 'count' => $count]);
        if(count($store) > 0) {
            $store = arraySort($store,'count');
        }
        return $store;
    }

    // 新客订单
    public function getNewUserByOrder($store_id,$start_time,$end_time)
    {
        $where = [];
        if (!$start_time) {
            $start_time = strtotime("-1 month");
            $end_time = time();
            $where[] = ['pay_time', 'egt', $start_time];
            $where[] = ['pay_time', 'elt', $end_time];
        } else {
            $where[] = ['pay_time', 'egt', strtotime($start_time." 00:00:00")];
            $where[] = ['pay_time', 'elt', strtotime($end_time." 23:59:59")];
        }
        if($store_id != -1) {
            $userInviteOne = UserInvitation::where('type',2)->select()->toArray();
            $idsOne = array_column($userInviteOne,'invitee');
            $whereFour = [];
            $array = [];
            $whereFour[] = ['id','in',$idsOne];
            $whereFour[] = ['status','eq',3];
            $whereFour[] = ['new_type','eq',1];
            $whereFour[] = ['store_id','eq',$store_id];
            $userOne = UserModel::where($whereFour)->select()->toArray();
            foreach ($userOne as $ka=>$va){
                $whereFive = $where;
                $whereFive[] = ['user_id','eq',$userOne[$ka]['id']];
                $whereFive[] = ['status', 'gt', 1];
                $list = ProductOrder::where($whereFive)->with('store,user')->select();
                if(!$list->isEmpty()) {
                    foreach ($list->toArray() as $k=>$v){
                        $invite = UserInvitation::where('invitee',$list[$k]['user_id'])->find();
                        if($invite['type'] == 1) {
                            $list[$k]['invite'] = UserModel::get($invite['inviter']);
                        }elseif($invite['type'] == 2){
                            $store_user = StoreUserModel::get($invite['inviter']);
                            $list[$k]['invite'] = UserModel::get($store_user['user_id']);
                            $list[$k]['invite']['nickname'] = $store_user['name'];
                            $list[$k]['invite']['phone'] = $store_user['phone'];
                        }elseif($invite['type'] == 3){
                            $team_user = TeamUserModel::get($invite['inviter']);
                            $list[$k]['invite'] = UserModel::get($team_user['user_id']);
                            $list[$k]['invite']['nickname'] = $team_user['name'];
                            $list[$k]['invite']['phone'] = $team_user['phone'];
                        }
                        $list[$k]['pay_time_int'] = strtotime($list[$k]['pay_time']);
                        array_push($array,$list[$k]);
                    }
                }
            }
        }else{
            $whereOne = [];
            $whereOne[] = ['type','eq',1];
            $userInvite = UserInvitation::where($whereOne)->select()->toArray();
            $ids = array_column($userInvite,'invitee');
            $whereTwo = [];
            $whereTwo[] = ['id','in',$ids];
            $whereTwo[] = ['status','eq',3];
            $user = UserModel::where($whereTwo)->select()->toArray();
            $array = [];
            foreach ($user as $ka=>$va){
                $whereThree = $where;
                $whereThree[] = ['user_id','eq',$user[$ka]['id']];
                $whereThree[] = ['status', 'gt', 1];
                $list = ProductOrder::where($whereThree)->with('store,user')->select();
                if(!$list->isEmpty()) {
                    foreach ($list->toArray() as $k=>$v){
                        $invite = UserInvitation::where('invitee',$list[$k]['user_id'])->find();
                        if($invite['type'] == 1) {
                            $list[$k]['invite'] = UserModel::get($invite['inviter']);
                        }elseif($invite['type'] == 2){
                            $store_user = StoreUserModel::get($invite['inviter']);
                            $list[$k]['invite'] = UserModel::get($store_user['user_id']);
                            $list[$k]['invite']['nickname'] = $store_user['name'];
                            $list[$k]['invite']['phone'] = $store_user['phone'];
                        }elseif($invite['type'] == 3){
                            $team_user = TeamUserModel::get($invite['inviter']);
                            $list[$k]['invite'] = UserModel::get($team_user['user_id']);
                            $list[$k]['invite']['nickname'] = $team_user['name'];
                            $list[$k]['invite']['phone'] = $team_user['phone'];
                        }
                        $list[$k]['pay_time_int'] = strtotime($list[$k]['pay_time']);
                        array_push($array,$list[$k]);
                    }
                }
            }
        }
        if(count($array) > 0) {
            $array = arraySort($array,'pay_time_int');
        }
        return $array;
    }
}